set allow_experimental_variant_type = 1;
set use_variant_as_common_type = 1;
set allow_experimental_dynamic_type = 1;


drop table if exists test;

{% for engine in ['MergeTree order by id settings min_rows_for_wide_part=1000000000, min_bytes_for_wide_part=10000000000',
                  'MergeTree order by id settings min_rows_for_wide_part=1000000000, min_bytes_for_wide_part=10000000000, vertical_merge_algorithm_min_rows_to_activate=1, vertical_merge_algorithm_min_columns_to_activate=1',
                  'MergeTree order by id settings min_rows_for_wide_part=1, min_bytes_for_wide_part=1',
                  'MergeTree order by id settings min_rows_for_wide_part=1, min_bytes_for_wide_part=1, vertical_merge_algorithm_min_rows_to_activate=1, vertical_merge_algorithm_min_columns_to_activate=1'] -%}

create table test (id UInt64, d Dynamic(max_types=2)) engine={{ engine }};

system stop merges test;
insert into test select number, number from numbers(10);
insert into test select number, 'str_' || toString(number) from numbers(8);
insert into test select number, range(number % 10 + 1) from numbers(7);
insert into test select number, toDate(number) from numbers(6);
insert into test select number, toDateTime(number) from numbers(5);
insert into test select number, NULL from numbers(10);

select count(), dynamicType(d), isDynamicElementInSharedData(d) from test group by dynamicType(d), isDynamicElementInSharedData(d) order by count(), dynamicType(d);
system start merges test; optimize table test final;;
select '---------------------';
select count(), dynamicType(d), isDynamicElementInSharedData(d) from test group by dynamicType(d), isDynamicElementInSharedData(d) order by count(), dynamicType(d);

system stop merges test;
insert into test select number, map(number, number) from numbers(20);
select '---------------------';
select count(), dynamicType(d), isDynamicElementInSharedData(d) from test group by dynamicType(d), isDynamicElementInSharedData(d) order by count(), dynamicType(d);
system start merges test;
optimize table test final;
select '---------------------';
select count(), dynamicType(d), isDynamicElementInSharedData(d) from test group by dynamicType(d), isDynamicElementInSharedData(d) order by count(), dynamicType(d);

system stop merges test;
insert into test select number, tuple(number, number) from numbers(1);
select '---------------------';
select count(), dynamicType(d), isDynamicElementInSharedData(d) from test group by dynamicType(d), isDynamicElementInSharedData(d) order by count(), dynamicType(d);
system start merges test;
optimize table test final;
select '---------------------';
select count(), dynamicType(d), isDynamicElementInSharedData(d) from test group by dynamicType(d), isDynamicElementInSharedData(d) order by count(), dynamicType(d);

system stop merges test;
insert into test select number, 'str_' || number from numbers(3);
select '---------------------';
select count(), dynamicType(d), isDynamicElementInSharedData(d) from test group by dynamicType(d), isDynamicElementInSharedData(d) order by count(), dynamicType(d);
system start merges test;
optimize table test final;
select '---------------------';
select count(), dynamicType(d), isDynamicElementInSharedData(d) from test group by dynamicType(d), isDynamicElementInSharedData(d) order by count(), dynamicType(d);

drop table test;

{% endfor -%}
